package com.xmut.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmut.eduservice.entity.EduSubject;
import com.xmut.eduservice.entity.excel.ExcelSubjectData;
import com.xmut.eduservice.entity.subject.OneSubject;
import com.xmut.eduservice.entity.subject.TwoSubject;
import com.xmut.eduservice.listener.SubjectExcelListener;
import com.xmut.eduservice.mapper.EduSubjectMapper;
import com.xmut.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-08-02
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            // 文件输入流
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 课程分类列表，树形结构
    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        // 查询所有的一级分类
        QueryWrapper<EduSubject> queryWrapperOne = new QueryWrapper<>();
        queryWrapperOne.eq("parent_id", "0");
        // 方法一
//        List<EduSubject> eduSubjectsOne = baseMapper.selectList(queryWrapperOne);
        // 方法二
        List<EduSubject> listEduSubjectsOne = this.list(queryWrapperOne);
        // 查询所有的二级分类
        QueryWrapper<EduSubject> queryWrapperTwo = new QueryWrapper<>();
        queryWrapperOne.ne("parent_id", "0");
        List<EduSubject> listEduSubjectsTwo = this.list(queryWrapperTwo);

        // 创建list集合用于存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 封装一级分类
        // 遍历查询的一级分类，封装到另一个list
        for (int i = 0; i < listEduSubjectsOne.size(); i++) {
            EduSubject eduSubjectOne = listEduSubjectsOne.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            // 使用工具类进行拷贝
            BeanUtils.copyProperties(eduSubjectOne, oneSubject);

            finalSubjectList.add(oneSubject);

            List<TwoSubject> finalSubjectListTwo = new ArrayList<>();
            for (int j = 0; j < listEduSubjectsTwo.size(); j++) {
                EduSubject eduSubjectTwo = listEduSubjectsTwo.get(j);
                if (eduSubjectTwo.getParentId().equals(eduSubjectOne.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubjectTwo, twoSubject);
                    finalSubjectListTwo.add(twoSubject);
                }
            }
            oneSubject.setChildren(finalSubjectListTwo);
        }


        return finalSubjectList;
    }
}
